<template>
  <div>
    <Skeleton v-show="!dataInfo" showSkeleton='1'/>
    <div class="dynamics-page" v-if="dataInfo">
      <div class="top-cnt">
        <div class="top-link">
          <div class="title">
            <h3>数据总览</h3>
            <p>数据更新至 {{getData()}}</p>
          </div>
          <div
            class="new-msg"
            @click="goMessageList"
          >
            <img
              src="../../assets/img/home/icon7.png"
              alt=""
            >
            <div
              class="dot"
              v-show="dataInfo.unreadMessageCount "
            ></div>
          </div>
        </div>
        <div class="all-data">
          <div
            class="customer"
            @click="jump(1)"
          >
            <div class="count">
              <p>{{dataInfo.allDataVo.customerCount || 0}}</p>
              <span v-show="dataInfo.allAddDataVo.customerCount">+{{dataInfo.allAddDataVo.customerCount}}</span>
            </div>
            <p class="title">意向客户</p>
          </div>
          <div
            class="clue"
            @click="jump(2)"
          >
            <div class="count">
              <p>{{dataInfo.allDataVo.strangerCount || 0}}</p>
              <span v-show="dataInfo.allAddDataVo.strangerCount">+{{dataInfo.allAddDataVo.strangerCount}}</span>
            </div>
            <p class="title">访客数量</p>
          </div>
          <div
            class="dynamic"
            @click="jump(3)"
          >
            <div class="count">
              <p>{{dataInfo.allDataVo.dynamicCount || 0}}</p>
              <span v-show="dataInfo.allAddDataVo.dynamicCount">+{{dataInfo.allAddDataVo.dynamicCount}}</span>
            </div>
            <p class="title">动态数量</p>
          </div>
        </div>
      </div>
      <div
        class="report-form shadow"
        @click="goDataCount"
      >
        <div class="icon">
          <img
            src="../../assets/img/home/icon8.png"
            alt=""
          >
        </div>
        <div class="info">
          <p class="title">数据报表</p>
          <p
            class="new-add"
            v-if="dataInfo.todayCustomerCount"
          >今日新增{{dataInfo.todayCustomerCount}}位客户，快去查看更多数据</p>
          <p
            class="new-add"
            v-else
          >今日没有新增客户，快去查看其他数据</p>
        </div>
      </div>
      <div class="market-box shadow">
        <div class="title scale-1px-bottom">
          <img
            src="../../assets/img/home/icon3.png"
            alt=""
          > 楼盘数据
        </div>
        <div class="data-box">
          <div
            class="share"
            @click="jump(4)"
          >
            <p class="count">{{dataInfo.linkerShareDataVo.shareCount || 0}}</p>
            <p class="tips">分享次数</p>
          </div>
          <div
            class="dynamic"
            @click="jump(5)"
          >
            <p class="count">{{dataInfo.linkerShareDataVo.dynamicCount || 0}}</p>
            <p class="tips">动态数量</p>
          </div>
          <div
            class="clue"
            @click="jump(6)"
          >
            <p class="count">{{dataInfo.linkerShareDataVo.strangerCount || 0}}</p>
            <p class="tips">访客数量</p>
          </div>
        </div>
      </div>
      <div class="card-box shadow">
        <div class="title scale-1px-bottom">
          <img
            src="../../assets/img/home/icon4.png"
            alt=""
          > 名片数据
        </div>
        <div class="data-box">
          <div
            class="share"
            @click="jump(7)"
          >
            <p class="count">{{dataInfo.cardShareDataVo.shareCount || 0}}</p>
            <p class="tips">分享次数</p>
          </div>
          <div
            class="dynamic"
            @click="jump(8)"
          >
            <p class="count">{{dataInfo.cardShareDataVo.dynamicCount || 0}}</p>
            <p class="tips">动态数量</p>
          </div>
          <div
            class="clue"
            @click="jump(9)"
          >
            <p class="count">{{dataInfo.cardShareDataVo.strangerCount || 0}}</p>
            <p class="tips">访客数量</p>
          </div>
        </div>
      </div>
      <div class="article-box shadow">
        <div class="title scale-1px-bottom">
          <img
            src="../../assets/img/home/icon5.png"
            alt=""
          > 文章数据
        </div>
        <div class="data-box">
          <div
            class="share"
            @click="jump(10)"
          >
            <p class="count">{{dataInfo.articleShareDataVo.shareCount || 0}}</p>
            <p class="tips">分享次数</p>
          </div>
          <div
            class="dynamic"
            @click="jump(11)"
          >
            <p class="count">{{dataInfo.articleShareDataVo.dynamicCount || 0}}</p>
            <p class="tips">动态数量</p>
          </div>
          <div
            class="clue"
            @click="jump(12)"
          >
            <p class="count">{{dataInfo.articleShareDataVo.strangerCount || 0}}</p>
            <p class="tips">访客数量</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import dynamicsService from 'SERVICE/dynamicsService'
export default {
  data () {
    return {
      dataInfo: {
        articleShareDataVo: {},
        cardShareDataVo: {},
        linkerShareDataVo: {},
        allAddDataVo: {},
        allDataVo: {}
      }
    }
  },
  created () {
    // let data = window.sessionStorage.getItem('dataCenterInfo')
    // if (data) {
    //   this.dataInfo = JSON.parse(data)
    // }
    // this.getDataCenterInfo()
  },
  beforeRouteEnter (to, from, next) {
    // ...
    wx.hideOptionMenu();
    next();
  },
  beforeRouteLeave (to, from, next) {
    if (to.name === 'shareData') {

    } else if (to.name === 'activityTrack') {
      dynamicsService.updateDynamicsCollect(0)
    } else if (to.name === 'thread') {
      dynamicsService.updateDynamicsCollect(1)
    } else if (to.name === 'custom') {
      dynamicsService.updateDynamicsCollect(2)
    }
    next()
  },
  methods: {
    // 浏览器回退监听
    goBack () {
      setTimeout(()=>{WeixinJSBridge.call("closeWindow")}, 100);  
      wx.closeWindow()
      console.log("点击了返回",wx);
    },
    // 获取当前时间
    getData () {
      let date = new Date()
      let y = date.getFullYear()
      let m = date.getMonth() + 1
      let d = date.getDate()
      return `${y}年${m}月${d}日`
    },
    // 跳转到线索或者动态
    jump (index) {
      let obj = { name: '', query: { type: '' } }
      switch (index) {
        case 1:
          obj.name = 'custom'
          break;
        case 2:
          obj.name = 'thread'
          obj.query.total = this.dataInfo.allDataVo.strangerCount || 0
          break;
        case 3:
          obj.name = 'activityTrack'
          break;
        case 4:
          obj.name = 'houseShareList'
          obj.query.type = 0
          break;
        case 5:
          obj.name = 'shareData'
          obj.query.type = 0
          obj.query.activeIndex = 0
          break;
        case 6:
          obj.name = 'shareData'
          obj.query.type = 0
          obj.query.activeIndex = 1
          obj.query.total = this.dataInfo.linkerShareDataVo.strangerCount || 0
          break;
        case 7:
          obj.name = 'cardShareList'
          obj.query.type = 1
          break;
        case 8:
          obj.name = 'shareData'
          obj.query.type = 1
          obj.query.activeIndex = 0
          break;
        case 9:
          obj.name = 'shareData'
          obj.query.type = 1
          obj.query.activeIndex = 1
          obj.query.total = this.dataInfo.cardShareDataVo.strangerCount || 0
          break;
        case 10:
          obj.name = 'articleShareList'
          obj.query.type = 2
          break;
        case 11:
          obj.name = 'shareData'
          obj.query.type = 2
          obj.query.activeIndex = 0
          break;
        case 12:
          obj.name = 'shareData'
          obj.query.type = 2
          obj.query.activeIndex = 1
          obj.query.total = this.dataInfo.articleShareDataVo.strangerCount || 0
          break;
      }
      this.$router.push(obj)
    },
    // 获取首页数据
    getDataCenterInfo () {
      dynamicsService.getDynamicsCollect().then(res => {
        this.dataInfo = res
        // 缓存首页数据
        window.sessionStorage.setItem('dataCenterInfo', JSON.stringify(res))
      }).catch(() => { })
    },
    // 动态列表入口
    goMessageList () {
      this.$router.push('/dynamics/message/messageList')
    },
    // 今日待办入口
    goTodoList () {
      this.$router.push('/dynamics/todoList')
    },
    goDataCount () {
      this.$router.push('/dynamics/dataCount')
    }
  },
  mounted() {
    if (window.history && window.history.pushState) {
       var state = {
        title: "title",
        url: "#"
      };
      window.history.pushState(state, "title", "#");
      window.addEventListener('popstate', this.goBack, false);
    }
  },
  destroyed() {
    window.removeEventListener('popstate', this.goBack,false)
  }
}
</script>

<style lang="less" scoped>
.dynamics-page {
  height: 100%;
  overflow-y: scroll;
  font-size: 12px;
  .top-cnt {
    color: #fff;
    height: 200px;
    background: linear-gradient(141deg, rgba(0, 158, 230, 1) 0%, rgba(0, 122, 230, 1) 100%);
    .top-link {
      display: flex;
      padding: 24px 16px 32px;
      .title {
        flex: 1;
        h3 {
          font-size: 24px;
          font-weight: 600;
          height: 32px;
          line-height: 32px;
        }
        p {
          font-size: 12px;
          font-weight: 400;
          height: 16px;
          line-height: 16px;
          opacity: 0.6;
        }
      }
    }
    .new-msg {
      display: flex;
      img {
        width: 24px;
        height: 24px;
      }
      .dot {
        width: 9px;
        height: 9px;
        background: rgba(234, 77, 46, 1);
        border: 1px solid rgba(255, 255, 255, 1);
        border-radius: 50%;
        margin: 0 0 0 -5px;
      }
    }
    .all-data {
      padding: 0 24px;
      display: flex;
      .customer,
      .clue,
      .dynamic {
        font-family: 'DINCondensed-Bold';
        flex: 1;
        .count {
          display: flex;
          p {
            font-size: 28px;
            height: 28px;
            line-height: 28px;
          }
          span {
            font-size: 14px;
            line-height: 18px;
          }
        }
        .title {
          font-size: 12px;
          font-weight: 400;
          line-height: 16px;
          // padding-top: 5px;
          opacity: 0.6;
          font-family: 'PingFangSC-Regular';
        }
      }
    }
  }
  .shadow {
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 3px 6px 0px rgba(58, 76, 130, 0.07), 0px 2px 17px 0px rgba(34, 47, 85, 0.05);
    border-radius: 10px;
  }
  .report-form,
  .todo-list {
    height: 84px;
    margin: -33px 16px 16px;
    display: flex;
    .icon {
      width: 37px;
      height: 58px;
      overflow: hidden;
      margin: 16px 16px 10px 25px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .info {
      margin-top: 21px;
      .title {
        font-size: 18px;
        color: #333;
        font-weight: 600;
        line-height: 26px;
      }
      .new-add {
        font-size: 12px;
        color: #9ca5b5;
        line-height: 17px;
        font-weight: 400;
      }
    }
  }
  .market-box,
  .card-box,
  .article-box {
    height: 128px;
    margin: 16px;
    .title {
      padding: 11px 16px;
      font-size: 16px;
      color: #333;
      // border-bottom: 1px solid rgba(228, 230, 240, 0.5);
      font-weight: 600;
      display: flex;
      align-items: center;
      img {
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
    }
    .data-box {
      display: flex;
      .share,
      .dynamic,
      .clue {
        flex: 1;
        padding-top: 22px;
        text-align: center;
        .count {
          font-size: 28px;
          font-family: 'DINCondensed-Bold';
          color: #40516f;
          height: 28px;
          line-height: 28px;
        }
        .tips {
          font-size: 12px;
          line-height: 16px;
          font-weight: 400;
          color: #666;
          padding-top: 3px;
        }
      }
      .clue {
        .count,
        .tips {
          color: #e65e2e;
        }
      }
    }
  }
  .todo-list {
    margin: 16px 16px 32px;
    .info {
      .new-add {
        color: #e65e2e;
      }
    }
  }
}
</style>
